package com.cadre.web.controller.officer;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.utils.R;
import com.cadre.common.utils.annotation.SysLog;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.ValidatorUtils;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A79;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A79Service;
import com.cadre.system.service.DIC_ZBService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Api(value = "家庭成员Controller", tags = {"家庭成员接口"})
@RequestMapping({"/profess/a79"})
@RestController
/* loaded from: A79Controller.class */
public class A79Controller {
    @Autowired
    private A79Service a79Service;
    @Resource
    private DIC_ZBService dIC_ZBService;
    @Autowired
    RestHighLevelClient client;

    @PostMapping({"/editA79Msg/{personid}"})
    @Transactional
    @ApiOperation("操作A79")
    public R editA79Msg(@RequestBody List<A79> list, @PathVariable("personid") String personid) throws IOException {
        List<A79> a79List = new ArrayList<>();
        if (list.size() > 0) {
            if (!(personid == null || personid == "")) {
                this.a79Service.deleteByPersonId(personid);
            }
            for (A79 entity : list) {
                entity.setFlowid(GenerateUUID.getUUID());
                this.a79Service.insertForlog(entity);
                a79List.add(entity);
            }
        } else if (!(personid == null || personid == "")) {
            this.a79Service.deleteByPersonId(personid);
        }
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, personid), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            a01s.setA79List(a79List);
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }

    @GetMapping({"/list/{personId}"})
    @ApiOperation("获取家庭成员列表")
    public R list(@PathVariable String personId) {
        return R.ok().put("page", (Object) this.a79Service.selectList(personId));
    }

    @GetMapping({"/info/{flowId}"})
    @ApiOperation("获取成员信息")
    public R info(@PathVariable("flowId") String flowId) {
        return R.ok().put("a79", (Object) ((A79) this.a79Service.getBaseMapper().selectById(flowId)));
    }

    @PostMapping({"/save"})
    @SysLog(value = "新增家庭成员信息", target = "A01", set = "A79", type = 0)
    @ApiOperation("添加成员")
    public R save(@RequestBody A79 a79) {
        a79.setFlowid(GenerateUUID.getUUID());
        this.a79Service.getBaseMapper().insert(a79);
        return R.ok();
    }

    @PostMapping({"/update"})
    @SysLog(value = "修改家庭成员信息", target = "A01", set = "A79", type = 1)
    @ApiOperation("成员信息修改")
    public R update(@RequestBody A79 a79) {
        ValidatorUtils.validateEntity(a79, new Class[0]);
        a79.setSerial(((A79) this.a79Service.getBaseMapper().selectById(a79.getFlowid())).getSerial());
        this.a79Service.getBaseMapper().updateById(a79);
        return R.ok();
    }

    @PostMapping({"/delete"})
    @SysLog(value = "删除家庭成员信息", target = "A01", set = "A79", type = DateUtil.SUB_MONTH)
    @ApiOperation("删除成员")
    public R delete(@RequestBody String[] personids) {
        this.a79Service.getBaseMapper().deleteBatchIds(Arrays.asList(personids));
        return R.ok();
    }

    @GetMapping({"/getA7910"})
    @ApiOperation("与本人关系字典")
    public R getA7910() {
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", "dic_gb4761");
        return R.ok().put("data", (Object) this.dIC_ZBService.getAll(map));
    }

    @GetMapping({"/getA7914"})
    @ApiOperation("政治面貌字典")
    public R getA7914() {
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", "dic_gb4762");
        return R.ok().put("data", (Object) this.dIC_ZBService.getAll(map));
    }

    @PostMapping({"/updateOrder"})
    @ApiOperation("家庭成员批量排序")
    public R updateOrder(@RequestBody List<A79> entityList) {
        if (entityList.size() > 0) {
            for (int i = 0; i < entityList.size(); i++) {
                this.a79Service.updateByPrimaryKeySelective(entityList.get(i));
            }
        }
        return R.ok("操作成功!");
    }

    @PostMapping({"/updateForIsout/{personid}"})
    @ApiOperation("家庭成员批量输入")
    public R saveA0195(@RequestBody A79[] a79, @PathVariable String personid) {
        List<A79> listA79 = this.a79Service.getBaseMapper().selectList(new LambdaQueryWrapper<A79>().eq(A79::getPersonid, personid));
        for (int n = 0; n < listA79.size(); n++) {
            listA79.get(n).setIsout((short) 0);
            this.a79Service.updateById(listA79.get(n));
        }
        for (A79 a792 : a79) {
            A79 a = (A79) this.a79Service.getBaseMapper().selectById(a792.getFlowid());
            a.setIsout((short) 1);
            this.a79Service.updateById(a);
        }
        return R.ok();
    }
}
